import Taro, { Component } from '@tarojs/taro'
import { View, Text, Image, Button } from '@tarojs/components'
import { uploadSubscribe, downloadSubscribe } from '../../api'
import './index.less'

let videoAd = null
export default class Index extends Component {

  config = {
    navigationBarTitleText: '云备份'
  }
  componentDidMount() {
    if (Taro.createRewardedVideoAd) {
      console.log('ssss')
      videoAd = Taro.createRewardedVideoAd({
        adUnitId: 'adunit-2663be541c182a88'
      })
      videoAd.onLoad(() => {
        //_fun()
      
      })
      videoAd.onError((err) => {
         // this.uploadSubscribe()
         console.log('onError')
      })
      videoAd.onClose((res) => { 
        console.log('onClose')
       
      })
      

    }
  }
  adShow(){
    videoAd.show().then(res=>{
      this.uploadSubscribe()
    }).catch(()=>{
      this.uploadSubscribe()
    })
  }
  componentDidShow() {
    const my_subscribe = Taro.getStorageSync('my_subscribe') || []
    this.setState({
      list: my_subscribe
    })
  }
  state = {
    list: []
  }
  uploadSubscribe() {
    if(this.state.list.length==0){
      Taro.showToast({
        title:'没有上传的订阅',
        icon:'none'
      })
      return
    }
   // const _fun = () => {
      uploadSubscribe({
        list: this.state.list
      }).then(res=>{
        Taro.showToast({
          icon:'success',
          title:"备份成功"
        })
      })
  //  }
    // 在页面onLoad回调事件中创建激励视频广告实例
    
  }
  onShareAppMessage() {
    return {
      title: 'RSS阅读器',
      desc: '提升阅读效率',
      imageUrl: require('../../assets/img/title.jpg'),
      path: '/pages/index/index'
    }
  }
  downloadSubscribe() {
    downloadSubscribe().then(res => {
      // console.log('res',res)
      if (res.list) {
        Taro.setStorageSync('my_subscribe', res.list)
        this.setState({
          list: res.list
        })
      }
      Taro.showToast({
        icon:'success',
        title:"下载成功"
      })
    })
  }
  render() {
    const { list } = this.state
    return <View className='  bg-white  radius shadow-lg'>
      <View className='cu-bar bg-white solid-bottom margin-top'>
        <View className='action'>
          <Text className='cuIcon-titles lg  text-green'></Text>
              列表


      </View>
        <View className='action'>
          <Button onClick={() => this.adShow()} className='cu-btn bg-green round sm button-hover'>上传订阅</Button>
          <Button onClick={() => this.downloadSubscribe()} className='cu-btn bg-green round sm button-hover'>下载订阅</Button>
        </View>
      </View>
      <View className='cu-list  menu-avatar menu'>
        {
          list.length < 1 ?

            <View className='text-center text-grey cu-item'  >你还没有添加任何订阅，点击上方“添加”按钮添加自定义订阅或到订阅分类查看添加。</View>
            :
            list.map((item, index) => {


              return <View

                key={index} className='cu-item  '
                onLongPress={() => this.onLongPress(item, index)}

              >



                <View

                  className='bg-white cu-avatar round lg'
                >
                  <Image className='img' src={require('../../assets/img/rss.png')}></Image>
                </View>
                <View className='content'>
                  {item.title}
                  <View className='text-grey' ></View>
                  <View className='text-gray text-sm'>{item.desc}
                  </View>
                </View>
                <View className='action'>

                </View>

              </View>
            })


        }

      </View>
    </View>
  }
}